package com.cn.ccp.action.classschedule;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.ClassScheduleBean;
import com.cn.ccp.bean.GradeBean;
import com.cn.ccp.bean.SpecialtyBean;
import com.cn.ccp.bean.TermSettingBean;

/**
 * 课程表action
 * @author renyongchengO
 *O
 */
public class ClassScheduleAction extends BaseAction{
	private Logger log = Logger.getLogger(ClassScheduleAction.class);
    private ClassScheduleBean classScheduleBean;
	private List<ClassScheduleBean> classScheduleList;
	private SelectItem[] specialtyItem ;//专业下拉框值
	private SelectItem[] gradeSelectItem ;// 班级下拉框值
	private SelectItem[] termSelectItem ;// 学期下拉框值
	private List<String> gradeList;
	
	public ClassScheduleAction(){
		log.info("------ClassScheduleAction()-------");
		initSpecialty();
		initTerm();
	}
	
	public void getpagedetail() {
		setItemCount(getClassScheduleService().getCount());
		int currentpage = getCurrentPage();
		int pagesize = getPageSize();
		int start = (currentpage - 1) * pagesize;
		classScheduleList = getClassScheduleService().pageQuery(start, pagesize);
	}
	
	
	private void initTerm(){
		int curYear = Calendar.getInstance().get(Calendar.YEAR);
		log.info("----------curYear = "+curYear);
		List<TermSettingBean> specialtyList = getTermSettingService().queryTerymByYear(curYear);
		if(specialtyList == null){
			termSelectItem = new SelectItem[0];
		}else{
			termSelectItem = new SelectItem[specialtyList.size()];
			for(int i = 0 ; i < specialtyList.size() ; i++){
				TermSettingBean bean = specialtyList.get(i);
				termSelectItem[i] = new SelectItem(String.valueOf(bean.getId() ), bean.getTermYear()+"["+bean.getTermTypeName()+"]");
			}
		}
	}
	
	
	private void initSpecialty(){
		List<SpecialtyBean> specialtyList = getSpecialtyService().pageQueryAllSpecialty();
		if(specialtyList == null){
			specialtyItem = new SelectItem[0];
		}else{
			specialtyItem = new SelectItem[specialtyList.size()];
			for(int i = 0 ; i < specialtyList.size() ; i++){
				SpecialtyBean bean = specialtyList.get(i);
				specialtyItem[i] = new SelectItem(String.valueOf(bean.getId() ), bean.getSpecialtyname());
//				log.info("-----------bean.getId() = ["+bean.getId()+"]");
//				if(i ==0 && getClassScheduleBean().getSpecialtyId() != null){
//					getClassScheduleBean().setSpecialtyId(""+bean.getId());
//				}
			}
		}
	}
	
	public String edit(){
		if(classScheduleBean.getSpecialtyId().equals("-1")){
			showMsg("specialty", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(gradeList.size() == 0 ){
			showMsg("number", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(classScheduleBean.getName().equals("")){
			showMsg("number", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		if(classScheduleBean.getTermSettingId().equals("-1")){
			showMsg("number", "javax.faces.component.UIInput.REQUIRED");
			return "";
		}
		log.info("---------------------------"+classScheduleBean);
		StringBuffer buf = new StringBuffer();
		int count = gradeList.size();
		for(int i=0; i<count;i++){
			if(i != count-1 && count !=1)
				buf.append(gradeList.get(i)+",");
			else
				buf.append(gradeList.get(i));
		}
		classScheduleBean.setGrade(buf.toString());
		if(classScheduleBean.getId() == 0)
			return save();
		return update();
	}
	public String save(){
		boolean flag = getClassScheduleService().save(classScheduleBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String update(){
		boolean flag = getClassScheduleService().update(classScheduleBean);
		if(flag){
			getpagedetail();
			return MAIN;
		}
		return "";
	}
	
	public String goUpdate(){
		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
		String id = varMap.get("id");
		classScheduleBean =  (ClassScheduleBean)getClassScheduleService().findClassScheduleById(Integer.parseInt(id));
		String grades = classScheduleBean.getGrade();
		gradeList = new ArrayList<String>();
		if(grades != null && !"".equalsIgnoreCase(grades)){
			String[] ar = grades.split(",");
			for(String a:ar)
				gradeList.add(a);
		}
		List<GradeBean> gradeList = getGradeService().getGradeBySpecialtyId(""+classScheduleBean.getSpecialtyId());
		gradeSelectItem = new SelectItem[gradeList.size()];
		for (int i = 0; i < gradeList.size(); i++)
			gradeSelectItem[i] = new SelectItem(""+gradeList.get(i).getId() , gradeList.get(i).getGradeType());
		
		return UPDATE;
	}
		
	public String detail(){
//		Map<String, String> varMap = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
//		String id = varMap.get("id");
//		log.info("=============:"+id);
		return "detail";
	}
	
	
	
	
	public void doDelete(){
		FacesContext context = FacesContext.getCurrentInstance();// 取得上下文
		String id = (String)context.getExternalContext().getRequestParameterMap().get("id");// 得到页面的参数
		getClassScheduleService().delete(Integer.parseInt(id));
		getpagedetail();
		log.info("-----------------doDelete()----------------"+id);
	}
	
	
	/**
	 * 选择的专业变化时
	 * 其对应的班级也要变化
	 * @param e
	 */
	public void changeSpecialty(ValueChangeEvent  e){
		String specialtyId = (String) e.getNewValue();
		log.info("----------specialtyId = '"+specialtyId);
		getClassScheduleBean().setSpecialtyId(specialtyId);
		List<GradeBean> splist = getGradeService().getGradeBySpecialtyId(specialtyId);
		
		if(classScheduleBean.getId() != 0){
			String[] strs = classScheduleBean.getGrade().split(",");
			gradeList = new ArrayList<String>();
			for (String s : strs) 
				gradeList.add(s);
		}
		
		gradeSelectItem = new SelectItem[splist.size()];
		for (int i = 0; i < splist.size(); i++) {
			gradeSelectItem[i] = new SelectItem(String.valueOf(splist.get(i).getId()),splist.get(i).getGradeType());
			log.info(splist.get(i).getId()+":"+splist.get(i).getGradeType());
		}
	}


 
	public List<ClassScheduleBean> getClassScheduleList() {
		getpagedetail();
		log.info("--------------------------classScheduleList.size() = "+classScheduleList.size());
		return classScheduleList;
	}


	public void setClassScheduleList(List<ClassScheduleBean> classScheduleList) {
		this.classScheduleList = classScheduleList;
	}


	public ClassScheduleBean getClassScheduleBean() {
		if(classScheduleBean == null)
			classScheduleBean = new ClassScheduleBean();
		return classScheduleBean;
	}


	public void setClassScheduleBean(ClassScheduleBean classScheduleBean) {
		this.classScheduleBean = classScheduleBean;
	}


	public SelectItem[] getSpecialtyItem() {
		return specialtyItem;
	}


	public void setSpecialtyItem(SelectItem[] specialtySelectItem) {
		this.specialtyItem = specialtySelectItem;
	}

	
	public SelectItem[] getGradeSelectItem() {
		if(gradeSelectItem == null)
			gradeSelectItem = new SelectItem[0];
		return gradeSelectItem;
	}


	public void setGradeSelectItem(SelectItem[] gradeSelectItem) {
		this.gradeSelectItem = gradeSelectItem;
	}


	public List<String> getGradeList() {
		if(gradeList == null)
			gradeList = new ArrayList<String>();
		return gradeList;
	}


	public void setGradeList(List<String> gradeList) {
		this.gradeList = gradeList;
	}

	public SelectItem[] getTermSelectItem() {
		return termSelectItem;
	}

	public void setTermSelectItem(SelectItem[] termSelectItem) {
		this.termSelectItem = termSelectItem;
	}
	
}
